<?php
namespace app\models;

class Posts extends IModel {
    public $table = 't_post';
    public $table_content = 't_post_content';

    public function insert($data) {
        $content = $data['content'];
        unset($data['content']);

        $k = array_keys($data);
        $v = array_values($data);

        // insert into t(title, post_time) value(?, ?)
        $fields = implode(',', $k);
        $valuesHolder = '';
        for($i=0,$len=count($k); $i<$len; $i++) {
            $valuesHolder .= '?,';
        }
        $valuesHolder = rtrim($valuesHolder, ',');

        // post
        $sql = 'INSERT INTO ' . $this->table . '('. $fields .') VALUES('. $valuesHolder .')';
        $this->db->prepareStatement($sql)->bindValues($v)->execute();
        $id = $this->db->getLastInsertId('id');

        // content
        $sql = 'INSERT INTO ' . $this->table_content . '(post_id, content) VALUES(?, ?)';
        $rows = $this->db->prepareStatement($sql)->bindValues([$id, $content])->execute();

        if($rows > 0) {
            return $id;
        }

        throw new \Exception('Insert post failed');
    }

    public function getOne($fields, $pkid) {
        $data = $this->db->createQuery()
            ->select($fields)
            ->from($this->table)
            ->where('id = ?', [$pkid])->getOne();

        // 删除的获取不到
        if(!$data || $data['status'] === '2') {
            return null;
        }

        $data['content'] = $this->db->createQuery()
            ->select('content')
            ->from($this->table_content)
            ->where('post_id = ?', [$data['id']])->getColumn();

        return $data;
    }

    public function isBelongTo($pkid, $uid) {
        $ret = $this->count('id=' . $pkid . ' and uid=' . $uid);

        if($ret > 0) {
            return true;
        }

        return false;
    }

    public function block($pkid, $status) {
        $sql = 'UPDATE ' . $this->table . ' set status=? where id=?';

        $this->db->prepareStatement($sql)->bindValues([$status, $pkid])->execute();
    }

    public function updateView($pkid) {
        $this->db->prepareSql('update '. $this->table .' set views=views+1 where id='. $pkid)->execute();
    }

    public function update($pkid, $data) {
        $content = $data['content'];
        unset($data['content']);

        $v = array_values($data);
        $v[] = $pkid;

        $ret = '';
        foreach($data as $key => $val) {
            $ret = $ret . $key . '=?,';
        }
        $ret = rtrim($ret, ',');

        // post
        $sql = 'UPDATE ' . $this->table . ' set ' . $ret . ' where id=?';
        $this->db->prepareStatement($sql)->bindValues($v)->execute();

        // content
        $sql = 'UPDATE ' . $this->table_content . ' set content=? where post_id=?';
        $this->db->prepareStatement($sql)->bindValues([$content, $pkid])->execute();
    }

    public function updateBasicInfo($pkid, $data) {
        $v = array_values($data);
        $v[] = $pkid;

        $ret = '';
        foreach($data as $key => $val) {
            $ret = $ret . $key . '=?,';
        }
        $ret = rtrim($ret, ',');

        // brief
        $sql = 'UPDATE ' . $this->table . ' set ' . $ret . ' where id=?';
        $this->db->prepareStatement($sql)->bindValues($v)->execute();
    }

    public function updateDetailInfo($pkid, $data) {
        $v = array_values($data);
        $v[] = $pkid;

        $ret = '';
        foreach($data as $key => $val) {
            $ret = $ret . $key . '=?,';
        }
        $ret = rtrim($ret, ',');

        // brief
        $sql = 'UPDATE ' . $this->table_content . ' set ' . $ret . ' where id=?';
        $this->db->prepareStatement($sql)->bindValues($v)->execute();
    }

    /**
     * 获取记录 包含被删除的内容
     */
    public function getOneWithoutCheck($fields, $pkid) {
        $data = $this->db->createQuery()
            ->select($fields)
            ->from($this->table)
            ->where('id = ?', [$pkid])->getOne();

        if(!$data) {
            return null;
        }

        return $data;
    }

    /**
     * 获取内容 包含被删除的内容
     */
    public function getContentWithoutCheck($fields, $pkid) {
        $data = $this->db->createQuery()
            ->select($fields)
            ->from($this->table_content)
            ->where('id = ?', [$pkid])->getOne();

        if(!$data) {
            return null;
        }

        return $data;
    }
}
